﻿
using System;
using System.Collections.ObjectModel;
using System.Linq;
using fc2editor.Common;
using fc2editor.DataModel.Descriptors;
namespace fc2editor.DataModel
{
    public class Pylon : NotificationObject
    {
        public int Index
        {
            get
            {
                return descriptor.Index;
            }
        }

        public ReadOnlyCollection<Weapon> Weapons
        {
            get
            {
                return weapons;
            }
            private set
            {
                weapons = value;
                NotifyPropertyChanged(() => Weapons);
            }
        }

        public float[] Coordinates
        {
            get
            {
                return descriptor.Coordinates;
            }
        }

        public float? Fiz
        {
            get
            {
                return descriptor.Fiz;
            }
        }

        public bool? Absent
        {
            get
            {
                return descriptor.Absent;
            }
        }

        public int? AbsentValue
        {
            get
            {
                return descriptor.AbsentValue;
            }
        }

        public Pylon(PylonDescriptor descriptor)
        {
            if (descriptor == null)
                throw new NullReferenceException("Illegal null-reference descriptor");

            this.descriptor = descriptor;
            CloneAndReparentWeapons(descriptor);
        }

        public void ReplaceWeapon(Weapon oldWeapon, Weapon newWeapon)
        {
            if (oldWeapon == null || newWeapon == null)
                return;

            if (oldWeapon.ID == newWeapon.ID)
                return;

            if (Weapons.Any(w => w.ID == oldWeapon.ID) && !Weapons.Any(w => w.ID == newWeapon.ID))
            {
                var wList = Weapons.ToList();
                int oldWeaponIndex = wList.IndexOf(oldWeapon);
                wList.Remove(oldWeapon);
                oldWeapon.SetParent(null);

                var wClone = newWeapon.Clone();
                wClone.SetParent(this);
                wList.Insert(oldWeaponIndex, wClone);

                Weapons = new ReadOnlyCollection<Weapon>(wList);
            }
        }

        public void RemoveWeapon(Weapon weapon)
        {
            if (weapon != null)
            {
                weapon.SetParent(null);

                var newWeapons = Weapons.ToList();
                newWeapons.Remove(weapon);
                Weapons = new ReadOnlyCollection<Weapon>(newWeapons);
            }
        }

        public void AddWeapon(Weapon weapon)
        {
            if (weapon != null)
            {
                bool exists = Weapons.Where(w => w.ID == weapon.ID).Any();
                if (!exists)
                {
                    var newWeapon = weapon.Clone();
                    newWeapon.SetParent(this);

                    var newWeapons = Weapons.ToList();
                    newWeapons.Add(newWeapon);
                    Weapons = new ReadOnlyCollection<Weapon>(newWeapons);
                }
            }
        }

        private void CloneAndReparentWeapons(PylonDescriptor descriptor)
        {
            var clonedWeapons = descriptor.Weapons.Select(w => w.Clone()).ToList();
            clonedWeapons.ForEach((w) => w.SetParent(this));
            Weapons = new ReadOnlyCollection<Weapon>(clonedWeapons);
        }

        private PylonDescriptor descriptor;
        private ReadOnlyCollection<Weapon> weapons;
    }
}
